// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.vehicles;

import java.nio.ByteBuffer;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

public class VehicleInterpolationTest extends Assert {
    final VehicleInterpolation interpolation = new VehicleInterpolation();
    final float[] physics = new float[27];
    final float[] engineSound = new float[2];
    final ByteBuffer bb = ByteBuffer.allocateDirect(255);
    final int tick = 100;
    final int delay = 300;
    final int history = 200;
    final int bufferingIterations = 4;
    @Rule
    public TestRule watchman = new TestWatcher() {
        protected void failed(Throwable var1, Description var2) {
            System.out.println("interpolation.buffer:");
            System.out.print("TIME: ");

            for (VehicleInterpolationData vehicleInterpolationData0 : VehicleInterpolationTest.this.interpolation.buffer) {
                System.out.print(String.format(" %5d", vehicleInterpolationData0.time));
            }

            System.out.println();
            System.out.print("   X: ");

            for (VehicleInterpolationData vehicleInterpolationData1 : VehicleInterpolationTest.this.interpolation.buffer) {
                System.out.print(String.format(" %5.0f", vehicleInterpolationData1.x));
            }
        }
    };

    @Before
    public void setup() {
        this.interpolation.clear();
        this.interpolation.delay = 300;
        this.interpolation.history = 500;
        this.interpolation.reset();
    }

    @Test
    public void normalTest() {
        long _long = 9223372036853775807L;

        for (int _int = 1; _int < 30; _int++) {
            this.bb.position(0);
            this.interpolation.interpolationDataAdd(this.bb, _long, (float)(_int * 2), (float)(_int * 2), 0.0F, _long);
            boolean boolean0 = this.interpolation.interpolationDataGet(this.physics, this.engineSound, _long - 298L);
            if (_int < 4) {
                assertFalse(boolean0);
            } else {
                assertTrue(boolean0);
                assertEquals((float)(_int - 4 + 1) * 2.0F, this.physics[0], 0.2F);
            }

            boolean boolean1 = this.interpolation.interpolationDataGet(this.physics, this.engineSound, _long - 298L + 50L);
            if (_int < 4) {
                assertFalse(boolean0);
            } else {
                assertTrue(boolean0);
                assertEquals((float)(_int - 4 + 1) * 2.0F + 1.0F, this.physics[0], 0.2F);
            }

            _long += 100L;
        }
    }

    @Test
    public void interpolationTest() {
        byte _byte = 0;

        for (int _int = 1; _int < 30; _int++) {
            this.bb.position(0);
            if (_int % 2 == 1) {
                this.interpolation.interpolationDataAdd(this.bb, (long)_byte, (float)_int, (float)_int, 0.0F, (long)_byte);
            }

            boolean _boolean = this.interpolation.interpolationDataGet(this.physics, this.engineSound, (long)(_byte - 298));
            if (_int < 4) {
                assertFalse(_boolean);
            } else {
                assertTrue(_boolean);
                assertEquals((float)(_int - 4 + 1), this.physics[0], 0.2F);
            }

            _byte += 100;
        }
    }

    @Test
    public void interpolationMicroStepTest() {
        byte _byte = 0;

        for (int int0 = 1; int0 < 30; int0++) {
            this.bb.position(0);
            this.interpolation.interpolationDataAdd(this.bb, (long)_byte, (float)int0, (float)int0, 0.0F, (long)_byte);
            boolean boolean0 = this.interpolation.interpolationDataGet(this.physics, this.engineSound, (long)(_byte - 298));
            if (int0 < 4) {
                assertFalse(boolean0);
            } else {
                assertTrue(boolean0);
                assertEquals((float)(int0 - 4 + 1), this.physics[0], 0.2F);
            }

            _byte += 100;
        }

        for (int int1 = 30; int1 < 35; int1++) {
            this.interpolation.interpolationDataAdd(this.bb, (long)_byte, (float)int1, (float)int1, 0.0F, (long)_byte);

            for (int int2 = 0; int2 < 100; int2++) {
                boolean boolean1 = this.interpolation.interpolationDataGet(this.physics, this.engineSound, (long)(_byte - 300 + 100 * int2 / 100));
                assertTrue(boolean1);
                assertEquals((float)(int1 - 4 + 1) + (float)int2 / 100.0F, this.physics[0], 0.001F);
            }

            _byte += 100;
        }
    }

    @Test
    public void interpolationMicroStepTest2() {
        long _long = 0L;
        byte _byte = 50;

        for (int int0 = 1; int0 < 30; int0++) {
            this.bb.position(0);
            this.interpolation.interpolationDataAdd(this.bb, _long, (float)int0, (float)int0, 0.0F, _long);
            boolean boolean0 = this.interpolation.interpolationDataGet(this.physics, this.engineSound, _long - 298L);
            System.out.println(int0 + "   " + _long + " " + boolean0 + " " + this.physics[0]);
            _long += (long)_byte;
        }

        for (int int1 = 30; int1 < 35; int1++) {
            this.interpolation.interpolationDataAdd(this.bb, _long, (float)int1, (float)int1, 0.0F, _long);

            for (int int2 = 0; int2 < 10; int2++) {
                boolean boolean1 = this.interpolation.interpolationDataGet(this.physics, this.engineSound, _long - 300L + (long)(_byte * int2 / 10));
                System.out
                    .println(
                        int1
                            + "."
                            + int2
                            + " "
                            + (_long + (long)(_byte * int2 / 10))
                            + " "
                            + boolean1
                            + " "
                            + this.physics[0]
                            + " "
                            + ((float)int1 - 6.0F + (float)int2 / 10.0F)
                    );
                assertTrue(boolean1);
                assertEquals((float)int1 - 6.0F + (float)int2 / 10.0F, this.physics[0], 0.001F);
            }

            _long += (long)_byte;
        }
    }

    @Test
    public void testBufferRestoring() {
        int int0 = 0;

        for (int int1 = 1; int1 < 30; int1++) {
            this.bb.position(0);
            this.interpolation.interpolationDataAdd(this.bb, (long)int0, (float)int1, (float)int1, 0.0F, (long)int0);
            boolean _boolean = this.interpolation.interpolationDataGet(this.physics, this.engineSound, (long)(int0 - 298));
            System.out.println(int1 + " " + int0 + " " + _boolean + " " + this.physics[0]);
            if (int1 >= 4 && (int1 <= 10 || int1 >= 14)) {
                assertTrue(_boolean);
                assertEquals((float)(int1 - 4 + 1), this.physics[0], 0.2F);
            }

            if (int1 == 10) {
                int0 += 500;
            }

            int0 += 100;
        }
    }

    @Test
    public void normalTestBufferRestoring2() {
        byte _byte = 0;

        for (int _int = 1; _int < 100; _int++) {
            this.bb.position(0);
            boolean boolean0 = _int < 15 || _int > 21;
            if (boolean0) {
                this.interpolation.interpolationDataAdd(this.bb, (long)_byte, (float)_int, 0.0F, 0.0F, (long)_byte);
            }

            boolean boolean1 = this.interpolation.interpolationDataGet(this.physics, this.engineSound, (long)(_byte - 298));
            System.out.println(_int + " " + boolean1 + " " + this.physics[0]);
            if (_int >= 4 && (_int <= 17 || _int >= 25)) {
                assertTrue(boolean1);
                if (_int >= 17 && _int <= 21) {
                    assertEquals(14.0F, this.physics[0], 0.1F);
                } else {
                    assertEquals((float)(_int - 4 + 1), this.physics[0], 0.1F);
                }
            } else {
                assertFalse(boolean1);
            }

            _byte += 100;
        }
    }

    @Test
    public void normalTestBufferRestoring3() {
        byte _byte = 0;

        for (int _int = 1; _int < 40; _int++) {
            this.bb.position(0);
            if (_int != 10 && _int != 12 && _int != 13 && _int != 15 && _int != 16) {
                this.interpolation.interpolationDataAdd(this.bb, (long)_byte, (float)_int, 0.0F, 0.0F, (long)_byte);
            }

            if (_int > 26 && _int < 33) {
                this.interpolation.interpolationDataAdd(this.bb, (long)(_byte + 50), (float)_int + 0.5F, 0.0F, 0.0F, (long)_byte);
            }

            boolean _boolean = this.interpolation.interpolationDataGet(this.physics, this.engineSound, (long)(_byte - 298));
            System.out.println(_int + " " + _boolean + " " + this.physics[0]);
            if (_int < 4) {
                assertFalse(_boolean);
            } else {
                assertTrue(_boolean);
                assertEquals((float)(_int - 4 + 1), this.physics[0], 0.1F);
            }

            _byte += 100;
        }
    }
}
